package com.aliyun.mts.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class StringUtil {

    /**
     * 获取UUID值
     * 
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 根据原格式,获取新的保存文件名(UUID)
     * 
     * @param fileName
     * @return
     */
    public static String getRandomName(String fileName) {
        int position = fileName.lastIndexOf(".");
        if (position > -1) {
            String suffix = fileName.substring(position, fileName.length());
            return getUUID() + suffix;
        }
        return null;
    }

    public static String parseBackslash(String path) {
        return path.replaceAll("\\\\", "/");
    }

    public static String getSuffix(String filePath) {
        if (filePath != null && filePath.indexOf(".") > 0) {
            return filePath.substring(filePath.lastIndexOf(".") + 1)
                    .toLowerCase();
        }
        return null;
    }

    /**
     * 去掉前后分隔符
     * 
     * @param path
     * @return
     */
    public static String parseUrlSeparator(String path) {
        if (path != null) {
            if (path.startsWith(File.separator)) {
                path = path.substring(1);
            }
            if (path.endsWith(File.separator)) {
                path = path.substring(0, path.length() - 1);
            }
        }
        return path;
    }

    /**
     * 解析一个html里面所有的图片路径到集合
     * 
     * @param html
     * @return
     */
    public static List<String> parseImgPath(String html) {
        List<String> paths = new ArrayList<String>(1);
        if (html != null && html.trim().length() > 0) {
            String[] imgs = html.split("<img src=\"");
            for (String img : imgs) {
                if (img != null && img.startsWith("http://")) {
                    img = img.substring(0, img.indexOf("\""));
                    paths.add(img);
                }
            }
        }
        return paths;
    }

    /**
     * 使用给定的分隔符拼接字符串
     * 
     * @param seperator
     * @param strings
     * @return
     */
    public static String appendStringBySeperator(String seperator,
            String... strings) {
        StringBuilder sb = new StringBuilder();
        for (String s : strings) {
            if (s != null && s.length() > 0) {
                sb.append(s).append(seperator);
            }
        }
        if (sb.length() > 0) {
            return sb.subSequence(0, sb.length() - 1).toString();
        }
        return null;
    }

    public static boolean isNull(String s) {
        if (s == null || s.trim().equals("")
                || s.toLowerCase().trim().equals("null")) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        // String dir = System.getProperty("user.dir");
        // System.out.println(dir);
        // String home = System.getProperty("user.home");
        // System.out.println(home);
        // String md5 = getFileMD5("F:\\temp\\MVN\\pom.xml");
        // System.out.println(md5);
        // System.out.println(getUUID());
        System.out.println(appendStringBySeperator(",", "", "2", "3"));
    }
}
